home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1991 / number3 / list.c < prev    next >
C/C++ Source or Header  |  1991-06-19  |  2KB  |  72 lines

  1. /* LIST.C
  2. usage: list [nodesize]
  3. Allocates and frees a lot of memory; uses linked list. 
  4. You can specify node size, default is 512 bytes
  5.  
  6. This is based on a sample routine distributed by Phar Lap as part
  7. of their DOS extender product.  Modified to work with the Watcom 
  8. heap system to illustrate 386 DOS extenders.  Used by permission.
  9.  
  10. Compile with the batch file SMAKE.BAT
  11. */
  12.  
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <malloc.h>
  16. #include <time.h>
  17. #include "heapstat.h"
  18.  
  19. typedef struct node {
  20.     unsigned long num;
  21.     void *data;
  22.     struct node *next;
  23.     } NODE;
  24.     
  25. main(int argc, char *argv[])
  26. {
  27.     HEAP_STATS h;
  28.     NODE *p, *q;
  29.     time_t t1, t2;
  30.     unsigned long nodes = 0;
  31.     unsigned nodesize = (argc > 1) ? atoi(argv[1]) : 512;
  32.     
  33.     time(&t1);
  34.     
  35.     for (q=NULL; ; q->next = p)
  36.     {
  37.         p = q;
  38.         if ((q = malloc(sizeof(NODE))) == NULL)
  39.             break;
  40.         if ((q->data = malloc(nodesize)) == NULL)
  41.         {
  42.             free(q);
  43.             break;
  44.         }
  45.         q->num = nodes++;
  46.         if ((nodes % 512) == 0)
  47.             printf("%lu nodes: %lu seconds\n", 
  48.                 nodes, time(&t2) - t1);
  49.     }
  50.         
  51.     printf("%lu nodes: %lu seconds\n", 
  52.         nodes, time(&t2) - t1);
  53.     printf("Allocated %uK\n", 
  54.         (nodes * (sizeof(NODE)+nodesize)) >> 10);
  55.     heap_stats(FARHEAP, &h);
  56.     print_heap_stats(&h);
  57.     
  58.     for ( ; p != NULL; p = q)
  59.     {
  60.         q = p->next;
  61.         if (p->num != --nodes)
  62.             printf("list corrupt: nodes=%lu num=%lu\n", 
  63.                 nodes, p->num);
  64.         free(p->data);
  65.         free(p);
  66.     }
  67.  
  68.     puts(nodes? "fail" : "ok");
  69.     return nodes;
  70. }
  71.  
  72.